﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace ee_wp_tourmaker.Views
{
    public partial class NewChallengeMapPage : PhoneApplicationPage
    {

        double lati = 0.0;
        double longi = 0.0;

        public NewChallengeMapPage()
        {
            InitializeComponent();

            searchLocationTextBox.Visibility = System.Windows.Visibility.Collapsed;
            this.ApplicationBar.IsVisible = true;
        }
        
        private void BingMap_Loaded(object sender, RoutedEventArgs e)
        {
            double lati1 = 59.4364700317383;
            double longi2 = 24.7525997161865;

            checkPointMap.Center = new GeoCoordinate(lati1, longi2);
            checkPointMap.ZoomLevel = 14;
        }

        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            if (lati != 0.0)
            {
                App.MainModel.SelectedCheckPoint.Latitude = (Decimal) lati;
                App.MainModel.SelectedCheckPoint.Longitude = (Decimal) longi;
            }
            NavigationService.GoBack();
        }

        private void ApplicationBarCancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
        /*
        private void checkPointMap_MouseClick(object sender, MapMouseEventArgs e)
        {
            Location p = checkPointMap.ViewportPointToLocation(e.ViewportPoint);

            lati = p.Latitude;
            longi = p.Longitude;
            checkPointMap.Children.Clear();
            Pushpin pin = new Pushpin();
            pin.Location = p;
            checkPointMap.Children.Add(pin);
        }
        */
        private void ApplicationBarSearchButton_Click(object sender, EventArgs e)
        {
            searchLocationTextBox.Visibility = System.Windows.Visibility.Visible;
            this.ApplicationBar.IsVisible = false;
        }

        private void searchLocationTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BingMapsTask bingMapsTask = new BingMapsTask();

                //Omit the Center property to use the user's current location.
                //bingMapsTask.Center = new GeoCoordinate(47.6204, -122.3493);

                bingMapsTask.SearchTerm = searchLocationTextBox.Text;
                bingMapsTask.ZoomLevel = 12;

                bingMapsTask.Show();

            }
        }


        private void mainMap_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var map = sender as Map;
            var point = e.GetPosition(map);
            var l = map.ViewportPointToLocation(point);
            // Center the map on a pushpin when touched.
            lati = l.Latitude;
            longi = l.Longitude;

            checkPointMap.Children.Clear();
            Pushpin pin = new Pushpin();
            pin.Background = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);
            pin.Location = l;
            checkPointMap.Children.Add(pin);
        }


    }
}